﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.IO;
namespace Editor
{
    public partial class Form1 : Form
    {
        private Graphics boardGraphicsControl;
        private Graphics _tileListGraphics;
        private Board _gameBoard;
        private Grid grid;
        private Image _Tiles;
        private Bitmap _Map;
        private Graphics _mapGraphics;
        private TileListWindow tileListWindow;
        private TileCreator tileCreatorWindow;
        private TileList _tileList;
        private CompleteTile _selectedTile = new CompleteTile();
        private const int ROWS = 20;
        private const int COLUMNS = 20;
        private const int IMAGESIZE = 32;
        public Form1()
        {
            Size cellSize = new Size(IMAGESIZE, IMAGESIZE);
            InitializeComponent();
            InitGraphicsData();
            _gameBoard = new Board(ROWS, COLUMNS);

            this.DoubleBuffered = true;
            LoadTiles();
            _tileList = new TileList(ref _tileListGraphics, cellSize, ref _gameBoard);
            grid = new Grid(ref boardGraphicsControl, ref _gameBoard, ref _tileList, cellSize, ROWS, COLUMNS);
            tileCreatorWindow = new TileCreator(ref _Tiles);
            tileListWindow = new TileListWindow(ref _Tiles);
            tileCreatorWindow.CreatedNewTile += new TileCreator.ReturnTile(_tileList.AddTile);
            panel1.MouseClick += new MouseEventHandler(_tileList.TileClicked);
            panel1.MouseDoubleClick += new MouseEventHandler(_tileList.TileDoubleClicked);
            panel1.MouseMove += new MouseEventHandler(_tileList.UpdateMousePosition);
            pictureBox1.MouseClick += new MouseEventHandler(grid.MapClicked);
            _tileList.EditTile += new TileList.ReturnTile(tileCreatorWindow.EditTile);
            tileCreatorWindow.UpdatedTile += new TileCreator.NulLDelegate(_tileList.UpdateTile);
            _tileList.CreateTile += new TileList.NullDelegate(CreateNewTile);
            vScrollBar1.Scroll += new ScrollEventHandler(grid.ScrollHeight);
            hScrollBar1.Scroll += new ScrollEventHandler(grid.ScrollWidth);
            tileCreatorWindow.Shown += new EventHandler(tileCreatorWindow_Shown);
            tileCreatorWindow.Hidden +=new TileCreator.NulLDelegate(tileCreatorWindow_Hidden);
            vScrollBar2.Scroll +=new ScrollEventHandler(_tileList.ScrollTileList);
        }

void  tileCreatorWindow_Hidden()
{
    bool value = true;
    panel1.Enabled = value;
    vScrollBar1.Enabled = value;
    hScrollBar1.Enabled = value;
    vScrollBar2.Enabled = value;
    pictureBox1.Enabled = value;
}

        void tileCreatorWindow_Shown(object sender, EventArgs e)
        {
            bool value = false;
            panel1.Enabled = value;
            vScrollBar1.Enabled = value;
            hScrollBar1.Enabled = value;
            vScrollBar2.Enabled = value;
            pictureBox1.Enabled = value;
        }

        private void InitGraphicsData()
        {
            _tileListGraphics = panel1.CreateGraphics();
            boardGraphicsControl = pictureBox1.CreateGraphics();
            _Map = new Bitmap(COLUMNS * IMAGESIZE, ROWS * IMAGESIZE);
            _mapGraphics = Graphics.FromImage(_Map);
            _tileListGraphics.Clear(Color.Black);
            boardGraphicsControl.Clear(Color.Black);
        }

        private void TileCreated(CompleteTile tile)
        {

        }

        private void LoadTiles()
        {
            Stream loader = this.GetType().Assembly.GetManifestResourceStream("Editor.Resources.dngn.jpg");
            _Tiles = Image.FromStream(loader);
            loader.Close();
        }

        private void MouseOverDrawingBox(object sender, MouseEventArgs e)
        {
            Pen _drawPen = new Pen(Color.Black);
            grid.UpdateMousePosition(e.Location);
            if (e.Button == MouseButtons.Left)
            {
                grid.AssignTile(e.Location);
            }
        }



        private void drawingBox_MouseClick(object sender, MouseEventArgs e)
        {
            switch (e.Button)
            {
                case MouseButtons.Left:
                    break;
                case MouseButtons.Right:
                    break;
                case MouseButtons.Middle:
                    break;
                default:
                    return;
            }
        }


        private void tilesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                tileListWindow.Show();
            }
            catch
            {
                tileListWindow = new TileListWindow(ref _Tiles);
                tileListWindow.Show();
            }
        }

        private void createNewTileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateNewTile();
        }

        private void CreateNewTile()
        {
            try
            {
                tileCreatorWindow.Show();
            }
            catch
            {
                tileCreatorWindow = new TileCreator(ref _Tiles);
                tileCreatorWindow.Show();
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            if (!e.Cancel)
            {
                FileStream FS = new FileStream(saveFileDialog1.FileName, FileMode.Create);
                _gameBoard.Save(ref FS);
            }
        }

        

    

    }
}
